let Common = require('JoystickType')

cc.Class({
    extends: cc.Component,

    properties: {

        ring: {
            default: null,
            type: cc.Node,
            displayName: 'ring',
        },

        ball: {
            default: null,
            type: cc.Node,
            displayName: 'ball',
        },
        
        touchType: {
            default: Common.TouchType.DEFAULT,
            type: Common.TouchType,
            displayName: 'TouchType',
        },   

        directionType: {
            default: Common.DirectionType.ALL,
            type: Common.DirectionType,
            displayName: 'Direction',
        },

        radius: {
            default: 100,
            displayName: 'radius',
        },

        normal_aplha: {
            default: 255,
            displayName: 'normal_aplha',
        },

        press_aplha: {
            default: 255,
            displayName: 'normal_aplha',
        },
    },

    onLoad() 
    {
        //this.ring.setPosition(0, 0)
        //this.ball.setPosition(0, 0)

        //let follow = this.touchType == Common.TouchType.FOLLOW

        this.node.opacity = this.normal_aplha

        let touchAt = null, stickAt = null

        this.node.on(
            cc.Node.EventType.TOUCH_START, 
            (e) =>{
           
                this.node.opacity = this.press_aplha

                touchAt = e.getLocation()

                stickAt = this.node.convertToNodeSpaceAR(e.getLocation())
            
                this.ring.setPosition(stickAt)
                this.ball.setPosition(stickAt)

                if (this.mStartCallback)
                {
                    this.mStartCallback(stickAt)
                }
            }, 
            this
        )

        let radius = this.radius > 0 ? this.radius : this.ring.width / 2

        this.node.on(
            cc.Node.EventType.TOUCH_MOVE, 
            (e) => 
            {
                if (!touchAt || !stickAt)
                {
                    return false
                }

                if (touchAt.equals(e.getLocation()))
                {
                    return false
                }

                let touchPos = this.ring.convertToNodeSpaceAR(e.getLocation())

                let dis = touchPos.mag()

                let x = stickAt.x + touchPos.x
                let y = stickAt.y + touchPos.y

                let angle  = this.angleBy(x, y)
                let radian = cc.macro.RAD * angle

                if(radius > dis)
                {
                    this.ball.x = x
                    this.ball.y = y
                }
                else
                {
                    this.ball.x = stickAt.x + Math.cos(radian) * radius
                    this.ball.y = stickAt.y + Math.sin(radian) * radius
                }

                if (this.mMoveCallback)
                {
                    this.mMoveCallback(radius, dis, radian)
                }
            }, 
            this)

        let endFunc = () =>{

            this.node.opacity = this.normal_aplha
            
            this.ball.x = this.ring.x
            this.ball.y = this.ring.y

            touchAt = null
            stickAt = null

            if (this.mEndCallback)
            {
                this.mEndCallback()
            }
        }

        this.node.on(cc.Node.EventType.TOUCH_END, endFunc, this)
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, endFunc, this)        
    },

    onDestroy()
    {
        this.mStartCallback = null
        this.mMoveCallback  = null
        this.mEndCallback   = null
        this.ring           = null
        this.ball           = null
    },

    angleBy(x, y)
    {
        return Math.atan2(y - this.ring.y, x - this.ring.x) * cc.macro.DEG
    },


    setCallbacks(start, move, end)
    {
        this.mStartCallback = start
        this.mMoveCallback  = move
        this.mEndCallback   = end
    }
});
